import { app, BrowserWindow, Tray, nativeImage, Menu, ipcMain} from 'electron';
import * as path from 'path';
import {Task, ConfApi,RuleApi} from './components/resource'
import {Sina} from './components/api/Sina'
import { MsgType } from './components';

let  contextMenu: Menu;
let tray: Tray;
let conf = new ConfApi();
let rule = new RuleApi();
let sina = new Sina();
let task: Task = new Task(conf, rule, sina);
let mainWindow:BrowserWindow = null;

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit();
}

const createWindow = (callback: () => void) => {
  // Create the browser window.
  mainWindow = new BrowserWindow({ width: 500
    , height: 400
    ,webPreferences: {nodeIntegration: true}
    ,resizable: false
    ,show: false
  });

  // and load the index.html of the app.
  mainWindow.loadFile(path.join(__dirname, '../src/index.html'));

  // Open the DevTools.
  mainWindow.webContents.openDevTools();
  mainWindow.show();

  mainWindow.webContents.once('dom-ready', () => {
    if (callback) {
      callback();
    }
  });
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
  createWindow(null);

  tray = new Tray(`${app.getAppPath()}/dist/resources/icons/png/16x16.png`)
  contextMenu = Menu.buildFromTemplate([
    { label: 'Turn Stock Cat On',
      type: 'radio',
      click: function() {
        handlerContextMenu("ON");
      }
    },
    { label: 'Turn Stock Cat Off',
      type: 'radio',
      click: function() {
        handlerContextMenu("OFF");
      }
    },
    { label: '规则',
      type: 'normal',
      click: function() {
        handlerContextMenu("RULE");
      },
    },
    { label: '设置', 
      type: 'normal' ,
      click: function() {
        handlerContextMenu("CONFIG");
      },
    },
    { label: '支持', 
      type: 'normal',
      click: function() {
        handlerContextMenu("SUPPORT");
      },
    },
    { label: '关于我们',
      type: 'normal',
      click: function() {
        handlerContextMenu("US");
      },
    },
  ]);
  tray.setToolTip('Stock cat 给你不一样的体验！');
  tray.setContextMenu(contextMenu);
  task.load();
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow(null);
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.





let handlerContextMenu = (type) => {
  if (type === "ON") {
    conf.enabled();
    if (mainWindow.isVisible()) {
      mainWindow.webContents.send(MsgType.CONFIG_UPDATE.toString(), type);
    }
    contextMenu.items[1].enabled = true
    contextMenu.items[0].enabled = false
    return;
  } else if ("OFF") {
    conf.disabled();
    contextMenu.items[1].enabled = false
    contextMenu.items[0].enabled = true
    if (mainWindow.isVisible()) {
      mainWindow.webContents.send("CONFIG_UPDATE", type);
    }
    return;
  }

  if (!mainWindow.isVisible()) {
    createWindow(() => {
      mainWindow.webContents.send("CONTEXT_MENU", type);
    });
  } else {
    mainWindow.show();
    mainWindow.webContents.send("CONTEXT_MENU", type);
  }
  
}

// 规则更新
ipcMain.on("RULE_UPDATE", () => {
  console.log("rule update");
  task.load();
});

// 配置更新
ipcMain.on("CONFIG_UPDATE", () => {
  task.load();

  if(conf.isEnabled()) {
    contextMenu.items[1].enabled = true
    contextMenu.items[0].enabled = false
  } else {
    contextMenu.items[1].enabled = false
    contextMenu.items[0].enabled = true
  }

});
